<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\api\model\v1;
use think\Model;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;
use Overtrue\EasySms\EasySms;
//use Overtrue\EasySms\PhoneNumber;
use think\facade\Db;

class Send extends Model{
    /**
     * 发送短信
     * mobile 接收短信的手机号码
     * content 短信内容
     * tempid 服务商短信模板ID
     * var_data 模板变量数组，使用在以模板ID来发送短信的平台
     * type 短信类型：reg注册，feedback留言
     * 腾讯云：返回值为0表示发送成功，否则失败
     * 本系统约定：1成功，0失败，-1非线上环境
    */
    static function sms($mobile,$content,$tempid,$var_data,$type){
        try {
            // 发送到国际码为 31 的国际号码
            // $mobile = new PhoneNumber($mobile, 31);
            $easySms = new EasySms(config('sms.sms'));
            $smsRsArr = $easySms->send($mobile,['content'=>$content,'template'=> $tempid,'data' => $var_data,]);
            $signid = 0;
            if(is_array($smsRsArr) and $smsRsArr['qcloud']['status'] == 'success') $signid = 1;
        } catch (\Exception $e) {
            // dd($e);
            $signid = '-1'; // 非线上环境
        }
        
        /*新增短信邮件日志*/
        if(config('sms.insert_database') == 1){
            switch ($type) {
                case 'feedback':
                    $log_content = $var_data[2];//留言id
                    $end_time = '';
                    break;
                default:
                    $minute = $var_data[1];
                    $log_content = $var_data[0];
                    $end_time = strtotime("+$minute minute");//30分钟内有效
                    break;
            }
            self::save_log($mobile, $end_time, $signid, $type, $log_content, 'mobile');
        }
        
        return $signid;
    }
    
    /**
     * 发送邮件
     * email 接收邮件的邮箱地址
     * temp_title 邮件标题
     * reply_mail 回信地址
     * send_name 回信地址用户名称
     * content 邮件正文
     * type 邮件类型：reg注册，feedback留言
     * 阿里云推送：成功返回发送成功的邮件条数，失败，返回false
     * 本系统约定：1成功，-1邮件配置错误
    */
    static function email($email,$temp_title,$reply_mail,$send_name,$content,$type,$log = null){
        $titleArr = explode('@',$temp_title);
        try {
            $transport = Transport::fromDsn('smtp://'.config('email.aliyun.username').':'.config('email.aliyun.password').'@'.config('email.aliyun.smtp').':'.config('email.aliyun.port'));
            $mailer = new Mailer($transport);
            $mail_detail = (new Email())
                ->from(config('email.aliyun.username'))
                ->to($email)
                ->replyTo($reply_mail)
                ->subject($titleArr[0].$titleArr[1])
                ->html($content);
            $mailer->send($mail_detail);
            $signid = 1;
        } catch (\Exception $e) {
            // dd($e);
            $signid = '-1';//邮件配置错误
        }
        
        /*新增短信邮件日志*/
        if(config('email.insert_database')){
            switch ($type) {
                case 'feedback':
                    $log_content = $log;//留言id
                    $end_time = '';
                    break;
                default:
                    $hour = $titleArr[2];
                    $log_content = $titleArr[1];
                    $end_time = strtotime("+$hour hour");//24小时内有效
                    break;
            }
            self::save_log($email, $end_time, $signid, $type, $log_content, 'email');
        }
        
        return $signid;
    }
    
    /*新增短信邮件日志*/
    static function save_log($account, $end_time, $signid, $type, $log_content, $log_type){
        $data['content'] = $log_content;
        $data['endtime'] = $end_time;
        $data['log_type'] = $log_type;
        $data['account'] = $account;
        $data['signid'] = $signid;
        $data['type'] = $type;
        $data['addtime'] = time();
        Db::name('log')->save($data);
    }

}